package com.joca.scheduling.hibernateservices;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.joca.scheduling.entities.Role;
import com.joca.scheduling.entities.Schedule;
import com.joca.scheduling.entities.User;
import com.joca.scheduling.factory.HibernateUtility;
import com.joca.scheduling.service.IUserServiceHibernate;

public class UserServiceHibernate implements IUserServiceHibernate {

	public static UserServiceHibernate userServiceHibernate;
	Session session;

	private UserServiceHibernate() {
		this.session = HibernateUtility.getSessionFactory().openSession();
		session.beginTransaction();

		session.createSQLQuery(
				"CREATE TABLE User (userID INTEGER NOT NULL IDENTITY,name VARCHAR(50),surname VARCHAR(50),username VARCHAR(50),password VARCHAR(50),role VARCHAR(50),PRIMARY KEY (userID))\n"
						+ "INSERT INTO USER VALUES(0,'Jovan','Dimitrijevic','joca.fabregas','scheduling','Professor')\n"
						+ "INSERT INTO USER VALUES(1,'Pera','Peric','pera','1','Student')\n"
						+ "CREATE TABLE Schedule (scheduleID INTEGER NOT NULL IDENTITY,description VARCHAR(255),reason VARCHAR(255),reject VARCHAR(255),scheduleRequest INTEGER,scheduleRespond INTEGER,scheduleDate DATE,PRIMARY KEY (scheduleID))"
						+ "INSERT INTO SCHEDULE VALUES(0, 'Opis zahteva studenta','Razlog zasto je prihvaceno','false',1,0,CURDATE())\n"
						+ "INSERT INTO SCHEDULE VALUES(1, 'Opis zahteva studenta','','true',1,0,NULL)\n"
						+ "INSERT INTO SCHEDULE VALUES(2, 'Opis zahteva studenta','Razlog zasto je odbijeno','true',1,0,NULL)")
				.executeUpdate();
		session.getTransaction().commit();
	}

	public static UserServiceHibernate getUserServiceHibernate() {
		if (userServiceHibernate == null)
			userServiceHibernate = new UserServiceHibernate();
		return userServiceHibernate;
	}

	public User addUser(User user) {
		session.save(user);
		return null;
	}

	public User logIn(User user) {
		List<User> users = (List<User>) session.createQuery(
				"from User user where user.username like '"
						+ user.getUsername() + "' and user.password like '"
						+ user.getPassword() + "'").list();
		if (users.isEmpty())
			return null;
		return users.get(0);
	}

	public List<User> getAllUsers() {
		session.beginTransaction();
		session.createQuery("from User");
		List<User> list = (List<User>) session.createQuery("from User").list();
		session.getTransaction().commit();
		return list;

	}

	public List<User> getAllUserProfessors() {
		List<User> listOfProfessors = new LinkedList<User>();
		List<User> listOfUsers = getAllUsers();
		for (User us : listOfUsers) {
			if (us.getRole() == Role.Professor)
				listOfProfessors.add(us);
		}
		return listOfProfessors;
	}

	/*
	 * public Collection<User> getAllUserProfessors() { Criteria criteria =
	 * session.createCriteria(User.class); return criteria.list(); }
	 */

	public User getUserByID(int userID) {
		return (User) session.get(User.class, userID);
	}

	public List<Schedule> getSchedulesRequests(User user) {
		List<Schedule> listOfRequests = new LinkedList<Schedule>();
		List<Schedule> listOfSchedules = getAllSchedules();

		for (Schedule sch : listOfSchedules) {
			if (user.getUserID() == sch.getScheduleRespond()
					&& sch.getReason().equals("")) {
				listOfRequests.add(sch);
			}
		}
		return listOfRequests;
	}

	public List<Schedule> getAllSchedules() {
		session.beginTransaction();
		session.createQuery("from Schedule");
		List<Schedule> list = (List<Schedule>) session.createQuery(
				"from Schedule").list();
		session.getTransaction().commit();
		return list;

	}

	public List<Schedule> getScheduleResponses(User user) {
		List<Schedule> listOfResponses = new LinkedList<Schedule>();
		List<Schedule> listOfSchedules = getAllSchedules();
		for (Schedule sch : listOfSchedules) {
			if (user.getUserID() == sch.getScheduleRespond()
					&& !sch.getReason().equals("")
					&& sch.getScheduleDate() != null) {
				listOfResponses.add(sch);
			}
		}
		return listOfResponses;
	}

	public List<Schedule> getScheduleResponsesForStudent(User user) {
		List<Schedule> listOfResponses = new LinkedList<Schedule>();
		List<Schedule> listOfSchedules = getAllSchedules();
		for (Schedule sch : listOfSchedules) {
			if (user.getUserID() == sch.getScheduleRequest()
					&& !sch.getReason().equals("")
					&& sch.getScheduleDate() != null) {
				listOfResponses.add(sch);
			}
		}
		return listOfResponses;
	}

	public Schedule getScheduleByID(int scheduleID) {
		return (Schedule) session.get(Schedule.class, scheduleID);

	}

	public User addSchedule(Schedule schedule) {
		session.save(schedule);
		return null;
	}

}
